<?php
namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\Admin as AdminModel;
use think\Exception;
use think\facade\Request;
use app\admin\validate\AddAdmin;

class Admin extends Base
{
    /*
     * 管理员列表
     */
    public function adminList(AdminModel $adminModel)
    {
        $datas = $adminModel->getPageList();

        $this->assign('datas',$datas);
        return $this->fetch('adminList');
    }

    /*
     * 管理员添加
     */
    public function addAdmin()
    {
        return $this->fetch('addAdmin');
    }

    /*
     * 删除管理员操作
     */
    public function delAdmin(AdminModel $adminModel)
    {
        $id = Request::param('id');
        $result = $adminModel->deleteAdmin($id);

        if ($result) {
            $this->success('删除成功','adminList');
        }

        $this->error('删除失败');
    }

    /*
     * 添加管理员操作
     */
    public function addAdminDo(AdminModel $adminModel, AddAdmin $addAdmin)
    {
        $params = Request::param();
        $rule = 'app\admin\validate\AddAdmin';
        $result = $this->validate($params,$rule);

        if (true !== $result) {
            $this->error($result);
        }

        try{
            $addAdmin->checkUserName($params['username']);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $res = $adminModel->addAdmin($params);
        if ($res) {
            $this->success('添加成功','adminList');
        }

        $this->error('添加失败');
    }
}